package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.mapper.QaAppearanceOptionMapper;
import com.yupi.springbootinit.model.dto.qa.QaAppearanceOptionAddRequest;
import com.yupi.springbootinit.model.dto.qa.QaAppearanceOptionQueryRequest;
import com.yupi.springbootinit.model.dto.qa.QaAppearanceOptionUpdateRequest;
import com.yupi.springbootinit.model.entity.QaAppearanceOption;
import com.yupi.springbootinit.service.QaAppearanceOptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【qaAppearanceOption(外观检查下拉选项)】的数据库操作Service实现
* @createDate 2024-02-25 17:04:02
*/
@Service
public class QaAppearanceOptionServiceImpl extends ServiceImpl<QaAppearanceOptionMapper, QaAppearanceOption>
        implements QaAppearanceOptionService {

    @Override
    public long addQaAppearanceOption(QaAppearanceOptionAddRequest qaAppearanceOptionAddRequest) {
        QaAppearanceOption qaAppearanceOption = new QaAppearanceOption();
        BeanUtils.copyProperties(qaAppearanceOptionAddRequest, qaAppearanceOption);
        this.save(qaAppearanceOption);
        return qaAppearanceOption.getId();
    }

    @Override
    public boolean deleteQaAppearanceOption(DeleteRequest deleteRequest) {
        return this.removeById(deleteRequest.getId());
    }

    @Override
    public boolean updateQaAppearanceOption(QaAppearanceOptionUpdateRequest qaAppearanceOptionUpdateRequest) {
        QaAppearanceOption qaAppearanceOption = new QaAppearanceOption();
        BeanUtils.copyProperties(qaAppearanceOptionUpdateRequest, qaAppearanceOption);
        return this.updateById(qaAppearanceOption);
    }

    @Override
    public QaAppearanceOption getQaAppearanceOptionById(long id) {
        return this.getById(id);
    }

    @Override
    public List<QaAppearanceOption> listQaAppearanceOption(QaAppearanceOptionQueryRequest qaAppearanceOptionQueryRequest) {
        QueryWrapper<QaAppearanceOption> queryWrapper = getQueryWrapper(qaAppearanceOptionQueryRequest);
        return this.list(queryWrapper);
    }

    @Override
    public Page<QaAppearanceOption> listQaAppearanceOptionByPage(QaAppearanceOptionQueryRequest qaAppearanceOptionQueryRequest) {
        long current = qaAppearanceOptionQueryRequest.getCurrent();
        long size = qaAppearanceOptionQueryRequest.getPageSize();
        Page<QaAppearanceOption> page = new Page<>(current, size);
        QueryWrapper<QaAppearanceOption> queryWrapper = getQueryWrapper(qaAppearanceOptionQueryRequest);
        return this.page(page, queryWrapper);
    }

    private QueryWrapper<QaAppearanceOption> getQueryWrapper(QaAppearanceOptionQueryRequest qaAppearanceOptionQueryRequest) {
        QueryWrapper<QaAppearanceOption> queryWrapper = new QueryWrapper<>();
        if (qaAppearanceOptionQueryRequest == null) {
            return queryWrapper;
        }
        Long id = qaAppearanceOptionQueryRequest.getId();
        if (id != null && id > 0) {
            queryWrapper.eq("id", id);
        }
        return queryWrapper;
    }
}